package org.java.documents.jdk.controller;

import org.java.commons.vo.Result;
import org.java.documents.jdk.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

//上传文件
@RestController
@RequestMapping("Upload")
public class UploadController {
    @Autowired
    private DocumentService documentService;
//MultipartFile一般是用来接受前台传过来的文件
    @PostMapping
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
//        使用流之后关闭
        try (InputStream in = file.getInputStream();) {
            String name = file.getName();
            String type = file.getContentType();
            long size = file.getSize();
            return this.documentService.upload(in, name, type, size);
        }
    }
}
